// initial state
import { StoreOptions } from "vuex";
import ACCESS_ENUM from "@/access/accessEnum";
import { UserControllerService } from "../../generated";

export default {
  namespaced: true,
  state: () => ({
    loginUser: {
      userName: "未登录",
      userRole: ACCESS_ENUM.NOT_LOGIN,
    },
  }),
  actions: {
    async getLoginUser({ commit, state }, payload) {
      // 从远程请求获取登录信息
      const res = await UserControllerService.getLoginUserUsingGet();
      console.log("getLoginUser 响应数据:", res); // 添加调试日志
      if (res.code === 0 && res.data) {
        const userData = {
          userName: res.data.userName || "未登录",
          userRole: res.data.userRole || ACCESS_ENUM.NOT_LOGIN,
        };
        console.log("准备更新的用户数据:", userData); // 添加调试日志
        commit("updateUser", userData);
      } else {
        commit("updateUser", {
          userName: "未登录",
          userRole: ACCESS_ENUM.NOT_LOGIN,
        });
      }
    },
  },
  mutations: {
    updateUser(state, payload) {
      console.log("更新前的状态:", state.loginUser); // 添加调试日志
      state.loginUser = payload;
      console.log("更新后的状态:", state.loginUser); // 添加调试日志
    },
  },
} as StoreOptions<any>;
